<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="请输入id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="活动标题" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['useActivity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['useActivity:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['useActivity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['useActivity:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="UseActivityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" align="center" label="序号"></el-table-column>
      <!-- <el-table-column label="记录ID" align="center" prop="id" />
      <el-table-column label="订单ID" align="center" prop="orderId" />       -->
      <el-table-column label="活动标题" align="center" prop="activity.activityName" />
      <el-table-column label="使用用户" align="center" prop="joinUser.userName" />
      <el-table-column label="活动类别" align="center" prop="activityType">
        <template slot-scope="scope">
          <span v-if="scope.row.activityType == 0">团建活动</span>
          <span v-else-if="scope.row.activityType == 1">亲子活动</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="使用数量" align="center" prop="useNumber" />
      <el-table-column label="使用时间" align="center" prop="useTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['useActivity:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['useActivity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引擎信息信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动ID" prop="activityId">
          <el-input v-model.trim.number="form.activityId" placeholder="请输入活动ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model.number="form.userId" placeholder="活动ID" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model.number="form.orderId" placeholder="订单ID" />
        </el-form-item>
        <el-form-item label="活动类型" prop="activityType">
          <el-select v-model="form.activityType" placeholder="活动类型" >
            <el-option label="团建活动" value="0" />
            <el-option label="亲子活动" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用数量" prop="useNumber">
          <el-input v-model.number="form.useNumber" placeholder="使用数量" />
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-date-picker
            v-model="form.useTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item> 
      </el-form>
      <div slot="footer" class="dialog-footer"  prop="status">  
        <el-button type="primary" @click="submitForm" >确 定</el-button>
        <el-button type="success" @click="temporarily">暂 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listUseActivity, getUseActivity, delUseActivity, addUseActivity, updateUseActivity } from "@/api/qysj/app/UseActivity.js";

export default {
  name: "UseActivity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引擎信息信息表格数据
      UseActivityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        activityName:null
        // auditId: null,
        // startTime: null,
        // endTime: null,
        // name: null,
        // createUserId: null,
        // type: null,
        // unit: null,
        // price: null,
        // status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activityId:[
          { required: true, message: "活动Id不能为空", trigger: "blur" },
          { type: 'number', message: "必须为数字" }
        ],
        userId:[
          { required: true, message: "用户Id不能为空", trigger: "blur" },
          { type: 'number', message: "必须为数字", trigger: "blur" }
        ],
        orderId:[
          { required: true, message: "订单Id不能为空", trigger: "blur" },
          { type: 'number', message: "必须为数字", trigger: "blur" }
        ],
        useNumber:[
          { required: true, message: "使用数量不能为空", trigger: "blur" },
          { type: 'number', message: "使用数量必须为数字", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询引擎信息信息列表 */
    getList() {
      this.loading = true;
      listUseActivity(this.queryParams).then(response => {
        this.UseActivityList = response.rows;
        console.log(this.UseActivityList);
        this.total = response.total;
        this.loading = false;
      });
    },
    getSingle() {
      this.loading = true;
      getUseActivity(this.queryParams.id).then(response => {
        if(response.data){
          this.UseActivityList = [response.data];
        }else{
          this.UseActivityList = [];
        }
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        auditId: null,
        startTime: null,
        endTime: null,
        name: null,
        createUserId: null,
        type: null,
        unit: null,
        price: null,
        remark: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    handleQueryAll() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getSingle();
    // },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQueryAll();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动使用记录";
    },
    //暂存按钮
    temporarily() {
      this.$refs["form"].validate(valid => {
        this.form.status = -1;
        console.log(this.form);
        if (valid) {
          if (this.form.id != null) {
            updateUseActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUseActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
        console.log("数据状态码为："+this.form.status);
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUseActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动使用记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUseActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUseActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log(row)
      this.$modal.confirm('是否确认删除活动标题为"' + row.activity.activityName + '"的数据项？').then(function() {
        return delUseActivity(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('useActivity/export', {
        ...this.queryParams
      }, `UseActivity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
